package com.fingard.dsp.bank.directbank.crbc01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalanceCheckConfirm;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.ReqBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.RetAtsBalCheckConfirmQry;
import com.fingard.dsp.bank.reqrespobj.balancecheckconfirm.detail.RetAtsBalCheckConfirmDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

public class CRBC01BalanceCheckConfirm extends CRBC01Base implements IBalanceCheckConfirm {
    @Override
    public RetAtsBalCheckConfirmQry d_7509_BalCheckConfirm(ReqBalCheckConfirmQry p_req7509) throws Exception {
        RetAtsBalCheckConfirmQry retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
        retAts.writeStart();
        try {
            CRBC01Header bankHeader = new CRBC01Header();
            bankHeader.tranCode = "140005";
            bankHeader.tranTime = new Date();
            while (p_req7509.readRecord()) {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("BODY");
                xmlWriter.writeElementString("ACCOUNT_NO", p_req7509.readedDetail.actNum);
                xmlWriter.writeElementString("STATE", p_req7509.readedDetail.dzFlag);
                xmlWriter.writeStartElement("ARRAY");
                xmlWriter.writeStartElement("STRUCT");
                xmlWriter.writeElementString("COMPARENO", p_req7509.readedDetail.statementNumber);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    retAts.writeRespCode("0");
                    StringBuilder tmpAbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpAbMsg);

                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                        String tmpRetMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                        if (isActiveRetCode(tmpRetCode)) {
                            RetAtsBalCheckConfirmDetail tmpRetDetail = new RetAtsBalCheckConfirmDetail();
                            while (tmpBkReader.read("/CRBANK/BODY")) {
                                if (tmpBkReader.readedXElement.getSingleChild("STATUS") != null) {
                                    tmpRetDetail.respInfo = tmpRetMsg;
                                    tmpRetDetail.bnkReqNo = tmpBkReader.readedXElement.getSingleText("HOSTFLOWNO");
                                    tmpRetDetail.statementNumber = p_req7509.readedDetail.statementNumber;
                                    tmpRetDetail.result = tmpBkReader.readedXElement.getSingleText("STATUS");
                                } else {
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.respInfo = "STATUS节点为空";
                                }
                            }
                            retAts.writeRetDetail(tmpRetDetail, p_req7509.isBatch);
                        } else {
                            retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                            retAts.writeStart();//写返回ATS的统一头部信息
                        }
                    } else {
                        retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                        retAts.writeStart();//写返回ATS的统一头部信息
                    }
                } else {
                    retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                    retAts.writeStart();//写返回ATS的统一头部信息
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                }
            }
        } catch (Exception e) {
            retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
/*
    public RetAtsBalCheckConfirmQry d_7509_BalCheckConfirm(ReqBalCheckConfirmQry p_req7509) throws Exception {
        RetAtsBalCheckConfirmQry retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
        retAts.writeStart();
        try {
            CRBC01Header bankHeader = new CRBC01Header();
            bankHeader.tranCode = "140006";
            bankHeader.tranTime = new Date();
            while (p_req7509.readRecord()) {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("BODY");
                xmlWriter.writeElementString("COMPARENO", p_req7509.readedDetail.statementNumber);
                xmlWriter.writeElementString("ACCOUNT_NO", p_req7509.readedDetail.actNum);
                xmlWriter.writeElementString("STATE", p_req7509.readedDetail.dzFlag);
                if ("1".equals(p_req7509.readedDetail.dzFlag) || "3".equals(p_req7509.readedDetail.dzFlag)) {
                    xmlWriter.writeElementString("NAME", "");
                    xmlWriter.writeElementString("POST", "");
                    xmlWriter.writeElementString("TELEPHONE", p_req7509.readedDetail.telNbr);
                    xmlWriter.writeElementString("PHONE", p_req7509.readedDetail.telNbr);
                    xmlWriter.writeElementString("EMAIL", "");
                    xmlWriter.writeElementString("REMARK", p_req7509.readedDetail.notConfirmReason);
                    xmlWriter.writeElementString("BANKBALANCE", p_req7509.readedDetail.custActBal);
                    xmlWriter.writeElementString("COMPANYBALANCE", p_req7509.readedDetail.custAmountIncurred);
                    xmlWriter.writeElementString("ADJUSTBALANCE", p_req7509.readedDetail.custAmountIncurred);
                    xmlWriter.writeElementString("COMPAREBALANCE", p_req7509.readedDetail.custAmountIncurred);
                }
                xmlWriter.writeStartElement("ARRAY");
                xmlWriter.writeStartElement("STRUCT");
                if ("1".equals(p_req7509.readedDetail.dzFlag) || "3".equals(p_req7509.readedDetail.dzFlag)) {
                    xmlWriter.writeElementString("DATE_D1", p_req7509.reqHeader.transTime);
                    xmlWriter.writeElementString("AMOUNT_D1", "");
                    xmlWriter.writeElementString("DATE_C1", p_req7509.reqHeader.transTime);
                    xmlWriter.writeElementString("AMOUNT_C1", "");
                    xmlWriter.writeElementString("DATE_D2", p_req7509.reqHeader.transTime);
                    xmlWriter.writeElementString("AMOUNT_D2", "");
                    xmlWriter.writeElementString("DATE_C2", p_req7509.reqHeader.transTime);
                    xmlWriter.writeElementString("AMOUNT_C2", "");
                }
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    retAts.writeRespCode("0");
                    StringBuilder tmpAbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpAbMsg);

                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_CODE");
                        String tmpRetMsg = tmpBkReader.getFirstText("/CRBANK/HEAD/RET_MSG");
                        if (isActiveRetCode(tmpRetCode)) {
                            RetAtsBalCheckConfirmDetail tmpRetDetail = new RetAtsBalCheckConfirmDetail();
                            while (tmpBkReader.read("/CRBANK/BODY")) {
                                if (tmpBkReader.readedXElement.getSingleChild("STATUS") != null) {
                                    tmpRetDetail.respInfo = tmpRetMsg;
                                    tmpRetDetail.bnkReqNo = tmpBkReader.readedXElement.getSingleText("HOSTFLOWNO");
                                    tmpRetDetail.statementNumber = p_req7509.readedDetail.statementNumber;
                                    tmpRetDetail.result = tmpRetMsg;
                                } else {
                                    tmpRetDetail.respCode = "0";
                                    tmpRetDetail.respInfo = "STATUS节点为空";
                                }
                            }
                            retAts.writeRetDetail(tmpRetDetail, p_req7509.isBatch);
                        } else {
                            retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                            retAts.writeStart();//写返回ATS的统一头部信息
                        }
                    } else {
                        retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                        retAts.writeStart();//写返回ATS的统一头部信息
                    }
                } else {
                    retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
                    retAts.writeStart();//写返回ATS的统一头部信息
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpStrRet[1]);
                }
            }
        } catch (Exception e) {
            retAts = new RetAtsBalCheckConfirmQry(p_req7509.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }
*/
}
